/*
 * MfClient.java
 *
 * Created on August 19, 2002, 12:27 AM
 */



import arc.mf.client.RemoteServer;
import arc.xml.XmlStringWriter;
import arc.xml.XmlDoc;
import arc.streams.LongInputStream;
import arc.streams.StreamCopy;

import java.util.Iterator;
import java.util.Collection;
import java.io.ByteArrayOutputStream;

/**
 * 
 * @author jason lohrey
 * @version
 * 
 *          This sample client illustrates making calles to Mediaflux to create
 *          an asset, execute a query and rerieve assets in the query.
 */
public class MediafluxClient {

	public MediafluxClient(MediafluxConfig mfConfig) throws Throwable {
		super();
		this.mfConfig = mfConfig;
		connect();
	}

	public MediafluxConfig getMfConfig() {
		return mfConfig;
	}
	
	MediafluxConfig mfConfig;
	RemoteServer.Connection cxn;

	private void connect() throws Throwable {
        // Create an instance of the server.
        RemoteServer server = new RemoteServer(mfConfig.getHost(),mfConfig.getPort(),mfConfig.isUseSSL(),mfConfig.isEncrypt());
        
        // Create a connection to the server. Connections are pooled, so they
        // should be closed when finished with. Alternatively, they may be
        // kept open as long as required.
        this.cxn = (RemoteServer.Connection)server.open();
        
        // We, the client, are required to log into the server before we can
        // execute services.
        this.cxn = (RemoteServer.Connection)server.open();
        
        this.cxn.connect(mfConfig.getDomain(),mfConfig.getUser(),mfConfig.getPassword());
	}

	/**
	 * Create an asset from a file.
	 */
	public void createAsset(String src, String namespace, String comment, String name) throws Throwable {

		// The asset create service can take one input for the asset. The
		// input is an arc.client.ServerClient.input which specifies the
		// MIME type (if known), appication data to be supplied to the server
		// which is used to populate content meta data and an
		// arc.streams.LongInputStream for the data source. The input can
		// be created manually, or there is a convenience function in
		// ServerClient to create from a URL:

		RemoteServer.Input in = RemoteServer.createInputFromURL(src);

		// We'd like to supply some arguments to 'asset.create'. Arguments
		// are defined in an XML document fragment, which is passed when
		// executing a service. The class arc.xml.XmlStringWriter is a useful
		// convenience class for efficiently creating XML documents.

		XmlStringWriter w = new XmlStringWriter();
		w.add("namespace", new String[] { "create", "yes" }, namespace);
		w.add("comment", comment);
		w.add("name", name);

		// Execute the service 'asset.create' with the arguments and the (data)
		// input.
		// There is no (data) output for this service.
		XmlDoc.Element reply = this.cxn.execute("asset.create", w.document(), in, null);

		// The reply is an XML document. We can ask for the value of the
		// asset identifier that was created.
		String id = reply.value("id");

		System.out.println("Created asset (id): " + id);
	}
	
	public boolean collectionExists(String namespace, String searchNamespace) throws Throwable {

		// Create the arguments document for the service. We'll ask for the
		// meta information for each asset to be returned - the default is to
		// simply return the asset identifier (which is equivalient to an
		// action of 'get-id'):
		XmlStringWriter w = new XmlStringWriter();
		w.add("namespace", namespace);

		// Execute the service 'asset.create' with the arguments and the (data)
		// input.
		// There is no (data) output for this service.
		XmlDoc.Element reply = this.cxn.execute("asset.namespace.list", w.document(), null, null);

		// Iterate through the results, and display each asset.
		Collection eles = reply.elements("namespace/namespace");
		if (eles == null) {
			return false;
		}

		Iterator it = eles.iterator();
		while (it.hasNext()) {
			XmlDoc.Element e = (XmlDoc.Element) it.next();
			if (e.value().equals(searchNamespace)) {
				return true;
			}
		}

		return false;
	}
	

}